﻿

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;
using System.Web;

namespace Viewer.BusinessObject.BusinessEntities
{
    [Serializable()]
    public partial class NSQuanHeGiaDinh : BusinessBase
    {
        CommonCode common = new CommonCode();
        DataClassesDataContext db = new DataClassesDataContext();


        public void EditQH(int hdId, string quanHe, DateTime ngay, string lyLich, string session,string hoten)
        {
            List<NSQuanHeGiaDinh> lst = (List<NSQuanHeGiaDinh>)HttpContext.Current.Session[session];
            NSQuanHeGiaDinh pc = lst.Where(a => a.QuanHeId.Equals(hdId)).SingleOrDefault();
            pc.TenMoiQuanHe = quanHe;
            pc.NamSinh = ngay;
            pc.LyLich = lyLich;
            pc.HoTenNguoiQuanHe = hoten;
        }
        public void DeleteQH(int hdId, string session)
        {
            List<NSQuanHeGiaDinh> lst = (List<NSQuanHeGiaDinh>)HttpContext.Current.Session[session];
            NSQuanHeGiaDinh pc = lst.Where(a => a.QuanHeId.Equals(hdId)).SingleOrDefault();
            lst.Remove(pc);
        }
        public void DeleletAllQHByNhanVien(int nhanVienId)
        {
            var query = (from a in db.NSQuanHeGiaDinhs
                         where a.NhanVienID.Equals(nhanVienId)
                         select a).ToList();
            if (query.Count > 0)
            {
                db.NSQuanHeGiaDinhs.DeleteAllOnSubmit(query);
                db.SubmitChanges();
            }

        }
        public void UpdateQH_NhanVien(int nhanVienId, List<NSQuanHeGiaDinh> QHObject,string typeQuanHe)
        {
            var queryQHByNhanVienId = new List<NSQuanHeGiaDinh>();
            if (typeQuanHe.Equals("QHBT"))
            {
                queryQHByNhanVienId = (from a in db.NSQuanHeGiaDinhs
                                       where a.NhanVienID.Equals(nhanVienId) && a.ModeQuanHe.Equals(0)
                                       select a).ToList();
            }
            else
            {
                queryQHByNhanVienId = (from a in db.NSQuanHeGiaDinhs
                                       where a.NhanVienID.Equals(nhanVienId) && a.ModeQuanHe.Equals(1)
                                       select a).ToList();
            }
          
            if (queryQHByNhanVienId.Count > 0)
            {
                db.NSQuanHeGiaDinhs.DeleteAllOnSubmit(queryQHByNhanVienId);
                db.SubmitChanges();
            }

            foreach (NSQuanHeGiaDinh pc in QHObject)
            {
                NSQuanHeGiaDinh item = new NSQuanHeGiaDinh();
                CopyNSQuanHeGiaDinh(pc, item);
                if (typeQuanHe.Equals("QHBT"))
                {
                    item.ModeQuanHe = 0;
                }
                else
                {
                    item.ModeQuanHe = 1;
                }
                db.NSQuanHeGiaDinhs.InsertOnSubmit(item);
            }
            db.SubmitChanges();

        }
        private void CopyNSQuanHeGiaDinh(NSQuanHeGiaDinh src, NSQuanHeGiaDinh des)
        {
            des.TenMoiQuanHe = src.TenMoiQuanHe;
            des.NamSinh = src.NamSinh;
            des.LyLich = src.LyLich;
            des.NhanVienID = src.NhanVienID;
            des.HoTenNguoiQuanHe = src.HoTenNguoiQuanHe;

        }
        public List<NSQuanHeGiaDinh> LoadQHByNhanVienId(int nhanVienId, string session)
        {
            var query = new List<NSQuanHeGiaDinh>();
            if (session.Equals("QHBT"))
            {
                query = (from a in db.NSQuanHeGiaDinhs
                         where a.NhanVienID.Equals(nhanVienId) && a.ModeQuanHe.Equals(0)
                         select a).ToList();
            }
            else if (session.Equals("QH"))
            {
                query = (from a in db.NSQuanHeGiaDinhs
                         where a.NhanVienID.Equals(nhanVienId) && a.ModeQuanHe.Equals(1)
                         select a).ToList();
            }


            return query;

        }
        public NSQuanHeGiaDinh GetQuanHe(int nhanVienId,string hoten,string tenQuanHe, DateTime ngayHieuLuc, string lyLich)
        {
            var db = new DataClassesDataContext();
            NSQuanHeGiaDinh hd = new NSQuanHeGiaDinh();
            hd.QuanHeId = 0 - DateTime.Now.Second;
            hd.NamSinh = ngayHieuLuc;
            hd.LyLich = lyLich;
            hd.TenMoiQuanHe = tenQuanHe;
            hd.NhanVienID = nhanVienId;
            hd.HoTenNguoiQuanHe = hoten;
            return hd;
        }
    }

}